import tkinter as tk
from tkinter import ttk
import yaml

saved=0

class SettingWindow:
    def __init__(self):
        self.combotrans = None
        self.combomode = None
        self.combooutput = None
        self.savebutton = None
        self.menubar = None
        self.config=None
        self.comboinput=None

        self.root = tk.Tk()
        self.root.attributes('-topmost', True)
        self.root.geometry("350x400+500+300")
        self.root.title("设置")

        self.setmode()

        self.root.mainloop()

    def main(self):
        self.savebutton = tk.Button(self.root, text="保存", command=self.save)
        self.savebutton.place(relx=0.5,rely=0.9,anchor=tk.CENTER)

    def setmode(self):
        with open('config.yaml', 'r') as file:
            self.config = yaml.load(file, Loader=yaml.FullLoader)
        self.clearall()
        self.main()

        label1 = tk.Label(text="选择输入模式")
        label1.pack()
        # 创建新的Combobox
        self.comboinput = ttk.Combobox(self.root)
        self.comboinput["values"] = ("长文本","剪贴板")
        self.comboinput["state"]='readonly'
        self.comboinput.current(self.config['input'])
        self.comboinput.pack(pady=20)
        label2 = tk.Label(text="选择输出模式")
        label2.pack()
        self.combooutput = ttk.Combobox(self.root)
        self.combooutput["values"] = ("气泡", "长文本")
        self.combooutput["state"] = 'readonly'
        self.combooutput.current(self.config['output'])
        self.combooutput.pack(pady=20)
        label3 = tk.Label(text="选择应答模式")
        label3.pack()
        self.combomode = ttk.Combobox(self.root)
        self.combomode["values"] = ("对话", "翻译")
        self.combomode["state"] = 'readonly'
        self.combomode.current(self.config['mode'])
        self.combomode.pack(pady=20)
        label4 = tk.Label(text="选择翻译模式")
        label4.pack()
        self.combotrans = ttk.Combobox(self.root)
        self.combotrans["values"] = ("翻译成英文", "翻译成中文")
        self.combotrans["state"] = 'readonly'
        self.combotrans.current(self.config['translate'])
        self.combotrans.pack(pady=20)
        # 绑定事件处理
        self.comboinput.bind("<<ComboboxSelected>>", self.input_select)
        self.combooutput.bind("<<ComboboxSelected>>", self.output_select)
        self.combomode.bind("<<ComboboxSelected>>", self.mode_select)
        self.combotrans.bind("<<ComboboxSelected>>", self.trans_select)

    def mode_select(self, event):
        self.config['mode'] = self.combomode.current()

    def input_select(self, event):
        self.config['input'] = self.comboinput.current()

    def output_select(self, event):
        self.config['output'] = self.combooutput.current()

    def trans_select(self, event):
        self.config['translate'] = self.combotrans.current()

    def save(self):
        with open('config.yaml', "w", encoding="utf-8") as f:
            yaml.dump(self.config, f)
        global saved
        saved=1

    def clearall(self):
        for widget in self.root.winfo_children():
            widget.destroy()


if __name__ == "__main__":
    app = SettingWindow()